package pl.nazaweb.jboot.afk;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Marek Kawczyński
 */
//fish 144s meat 180s
public class FoodEaterCallback implements ICallback {
    
    private Point foodPoint;
    private Robot robot;
    private FishingCallback fishingCallback;
    private Point keyboardControlPoint;
    
    public FoodEaterCallback(Point fodPoint) throws AWTException {
        this.foodPoint = fodPoint;
        robot = new Robot();
    }
    
        public FoodEaterCallback(Point fodPoint, Point keyboardControlPoint) throws AWTException {
        this.foodPoint = fodPoint;
        this.keyboardControlPoint = keyboardControlPoint;
        robot = new Robot();
    }
    
    @Override
    public void execute() {
        try {
            if(keyboardControlPoint!=null){
                    robot.mouseMove(keyboardControlPoint.x, keyboardControlPoint.y);
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            }else{
                robot.keyPress(KeyEvent.VK_CONTROL);
            }
            robot.mouseMove(foodPoint.x, foodPoint.y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            Thread.sleep(2000);
            if(keyboardControlPoint==null){
                robot.keyRelease(KeyEvent.VK_CONTROL);
            }
            if(keyboardControlPoint!=null){
                    robot.mouseMove(keyboardControlPoint.x, keyboardControlPoint.y);
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            }
    //        if(keyboardControlPoint!=null){
    //                robot.mouseMove(keyboardControlPoint.x, keyboardControlPoint.y);
    //                robot.mousePress(InputEvent.BUTTON1_MASK);
    //            robot.mouseRelease(InputEvent.BUTTON1_MASK);
    //        }
            if (fishingCallback != null) {
                fishingCallback.execute();
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(FoodEaterCallback.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public FishingCallback getFishingCallback() {
        return fishingCallback;
    }
    
    public void setFishingCallback(FishingCallback fishingCallback) {
        this.fishingCallback = fishingCallback;
    }
}
